/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: boot_manager.cpp
 *
 * Purpose: boot manager implementation
 *
 * Developer:
 *   wen.gu , 2019-09-29
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "boot_manager.h"

#include "panda/sys/property.h"    


#define LOG_TAG "btmg"
#include "panda/core/log.h"
#if 0
namespace panda
{
namespace system
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define BOOT_SRV_COM_TYPE "ro.srv.com_type"
#define BOOT_SRV_NAME "boot_manager"

#define BOOT_SRV_METHOD_REG(name, method) \
        mSrv->registerMethod(name, \
    [=](std::shared_ptr<ServiceJsonRequestHandle> reqHdl,\
        const std::string& methodStr, const Value& param) -> PandaErrc { \
        return method(reqHdl, methodStr, param);})

#define BOOT_SRV_METHOD_REG2(method) BOOT_SRV_METHOD_REG(#method, method)

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
BootManager::BootManager()
{
    /** todo something */
    //getenv();
}

BootManager::~BootManager()
{
    /** todo something */
}

bool BootManager::initialize()
{
    if (PropertyGet(BOOT_SRV_COM_TYPE, &mComType))
    {
        mSrv = new ServiceJson(BOOT_SRV_NAME, mComType);
        
        BOOT_SRV_METHOD_REG2(bootTarget);
        BOOT_SRV_METHOD_REG2(suspendTarget);
        BOOT_SRV_METHOD_REG2(resumeTarget);
        BOOT_SRV_METHOD_REG2(stopTarget);
        BOOT_SRV_METHOD_REG2(rebootTarget);
        BOOT_SRV_METHOD_REG2(addBootItem);
        BOOT_SRV_METHOD_REG2(removeBootItem);
        BOOT_SRV_METHOD_REG2(updateBootItem);
        BOOT_SRV_METHOD_REG2(queryBootItem);
        BOOT_SRV_METHOD_REG2(queryBootState);

        return true;
    }
    else
    {
        LOGE("get service communication type from system property failed\n");
    }

    return false;
}

bool BootManager::start()
{
    if (mSrv)
    {
        return (mSrv->start() == PandaErrc::OK);
    }

    return false;
}

bool BootManager::stop()
{
    if (mSrv)
    {
        return (mSrv->stop() == PandaErrc::OK);
    }

    return false;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/** boot a target
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *              [optional]   path:  the path of boot target
    *              [optional]   param: the boot parameter for target
    *              [optional]   log_redirect: redirect stdio output
    *              [optional]   dependence: the dependence for target.
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::bootTarget(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                  const std::string& method, const Value& param)
{

    return PandaErrc::OK;
}

/** suspend a target
* reqHdlr [in] response handle
* method  [in] method name
* param   [in] the parameter for request
*              [mandatory ] name:  the name of boot target
*
* reply   [out]: code : error code
*                descroption: the descriptor for error code
*/
PandaErrc BootManager::suspendTarget(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                     const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}

/** resume a target
* reqHdlr [in] response handle
* method  [in] method name
* param   [in] the parameter for request
*              [mandatory ] name:  the name of boot target
*
* reply   [out]: code : error code
*                descroption: the descriptor for error code
*/
PandaErrc BootManager::resumeTarget(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                    const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}


/** stop a target
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::stopTarget(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                  const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}

/** reboot a target
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::rebootTarget(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                    const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}

/** add a item to boot list
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *              [optional]   path:  the path of boot target
    *              [optional]   param: the boot parameter for target
    *              [optional]   log_redirect: redirect stdio output
    *              [optional]   dependence: the dependence for target.
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::addBootItem(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                   const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}


/** remove a item from boot list
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::removeBootItem(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                      const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}

/** update a boot item in boot list
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *              [optional]   path:  the path of boot target
    *              [optional]   param: the boot parameter for target
    *              [optional]   log_redirect: redirect stdio output
    *              [optional]   dependence: the dependence for target.
    *
    * reply   [out]: code : error code
    *                descroption: the descriptor for error code
    */
PandaErrc BootManager::updateBootItem(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                      const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}


/** query boot item from boot list
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *
    * reply   [out]: [mandatory ] code : error code
    *                [mandatory ] descroption: the descriptor for error code
    *                [mandatory ] name:  the name of boot target
    *                [optional]   path:  the path of boot target
    *                [optional]   param: the boot parameter for target
    *                [optional]   log_redirect: redirect stdio output
    *                [optional]   dependence: the dependence for target.
    */
PandaErrc BootManager::queryBootItem(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                     const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}

/** querty boot state
    * reqHdlr [in] response handle
    * method  [in] method name
    * param   [in] the parameter for request
    *              [mandatory ] name:  the name of boot target
    *
    * reply   [out]: [mandatory ] code : error code
    *                [mandatory ] descroption: the descriptor for error code
    *                [optional]   state: the state of current boot item
    *                             0: not boot yet
    *                             1: is running
    *                             2: suspend state.
    *
    */
PandaErrc BootManager::queryBootState(std::shared_ptr<ServiceJsonRequestHandle> reqHdl,
                                      const std::string& method, const Value& param)
{
    return PandaErrc::OK;
}


} /** namespace system */

} /** namespace panda */


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#endif
int main(int argc, char* argv[])
{

    return 0;
}
